package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.product.client.ProductClientFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author BrookPeng
 * @Date 2020/11/24 13:10
 * @Version 1.0
 */

@Slf4j
@Controller
public class IndexController {

    @Autowired
    private ProductClientFeign productClientFeign;

    /*// 进入首页 http://www.gmall.com
    // 生成的静态页面首页
    @GetMapping("/")
    public String index() {
        // 转发页面
        return "index";
    }*/

    // 查询数据库的首页
    @GetMapping("/")
    public String index(Model model) {

        List<Map<String, Object>> listMap = getData();

        model.addAttribute("list", listMap);

        return "index/index";
    }

    @Autowired
    // 模板引擎
    private TemplateEngine templateEngine; // 执行静态化的类

    // 人为触发 静态化程序进行生成静态化程序 首页
    @GetMapping("/createHtml")
    @ResponseBody
    public Result createHtml() {
        // 数据
        Context iContext = new Context();
        List<Map<String, Object>> data = getData();
        iContext.setVariable("list", data);
        // 写入流 注意不要乱码
        Writer writer = null;
        try {
            // writer = new FileWriter("D:\\temp\\index.html");
            // writer = new OutputStreamWriter(new FileOutputStream("D:\\temp\\index.html"), Charset.forName("UTF-8"));
            writer = new PrintWriter(new File("D:\\temp\\index.html"), "UTF-8");
            // 生成首页
            // param1:页面
            // param2:数据
            // param3:流 写入
            templateEngine.process("index/index.html", iContext, writer);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("静态页面生成异常,writer = {}", writer);
        } finally {
            try {
                if (null != writer)
                    writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return Result.ok();
    }

    private List<Map<String, Object>> getData() {

        // 现有的数据 一二三级分类关联数据 视图
        List<BaseCategoryView> categoryViewList = productClientFeign.getCategoryViewList();

        // 根据一级分类ID进行分组
        Map<Long, List<BaseCategoryView>> collect = categoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

        // 业务处理 自定义POJO 无敌版的POJO
        List<Map<String, Object>> listMap = new ArrayList<>();

        int index = 0;
        for (Map.Entry<Long, List<BaseCategoryView>> longListEntry1 : collect.entrySet()) {

            Map<String, Object> map = new HashMap<>();
            List<BaseCategoryView> value = longListEntry1.getValue();
            index++;
            map.put("index", index);
            Map<Long, List<BaseCategoryView>> collect2 = value.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            List<Map<String, Object>> listMap2 = new ArrayList<>();
            for (Map.Entry<Long, List<BaseCategoryView>> longListEntry2 : collect2.entrySet()) {

                Map<String, Object> map2 = new HashMap<>();
                List<BaseCategoryView> value2 = longListEntry2.getValue();

                List<Map<String, Object>> list3 = new ArrayList<>();
                for (BaseCategoryView longListEntry3 : value2) {

                    Map<String, Object> map3 = new HashMap<>();
                    map3.put("categoryName", longListEntry3.getCategory3Name());
                    map3.put("categoryId", longListEntry3.getCategory3Id());
                    list3.add(map3);
                }
                map2.put("categoryChild", list3);
                map2.put("categoryName", value2.get(0).getCategory2Name());
                map2.put("categoryId", longListEntry2.getKey());
                listMap2.add(map2);
            }
            map.put("categoryChild", listMap2);
            map.put("categoryName", value.get(0).getCategory1Name());
            map.put("categoryId", longListEntry1.getKey());
            listMap.add(map);
        }

        return listMap;
    }
}